package com.sdkj.action.service;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sdkj.Entity.service.News;
import com.sdkj.Entity.sys.Images;
import com.sdkj.action.base.BaseAction;
import com.sdkj.service.service.INewsService;
import com.sdkj.utils.CommonUtils;
import com.sdkj.utils.PageBean;


@Controller
@Scope("prototype")
public class NewsAction extends BaseAction<News, INewsService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public void saveNews() throws SQLException{
		model.setImg(new Images(imgid, new Date()));
		Serializable s=service.save(model);
		JSONObject res=new JSONObject(); 
		if(s!=null){  
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "保存新闻成功");
			res.put("id", s);
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "保存新闻失败");
		}
		
		try {
			operationLogService.save(getOperationLogBean("save news",new String[]{"content","sumary","subtitle"}));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		CommonUtils.writeJson(res);
	}
	
	public void updateNews() throws SQLException{
		model.setImg(new Images(imgid, new Date()));
		Serializable s=service.update(model);
		JSONObject res=new JSONObject(); 
		if(s!=null){  
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "修改新闻成功");
			res.put("id", s);
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "修改新闻失败"); 
		}
		try {
			operationLogService.save(getOperationLogBean("update news",new String[]{"content","sumary","subtitle"}));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		CommonUtils.writeJson(res);
	}
	
	
	public void list() throws SQLException{  
		String hql=" from News ";
		
		Map<String,Object>params=new HashMap<String,Object>();
		//1公司动态 2 行业动态
		if(StringUtils.isNotBlank(model.getType())){
			hql+=" where type=:t";
			params.put("t", model.getType());
		}
		hql+=" order by createTime desc";
		
		PageBean pb=new PageBean(page, service.count("select count(*) "+hql,params), rows);
		List<News>data=service.find(hql, params, page, rows);
		JSONObject res=new JSONObject();
		res.put(RES_CODE, 100);
		res.put(RES_MSG, "查询成功");
		res.put("pagebean", pb);
		JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
		  jsonConfig.setIgnoreDefaultExcludes(false);  //设置默认忽略
		  jsonConfig.setExcludes(new String[]{"img"});
		res.put("data", JSONArray.fromObject(data, jsonConfig));
		CommonUtils.writeJson(res);
	}
	
	public void details(){
		String hql=" from News where uuid = :id";
		Map<String,Object>params=new HashMap<String,Object>();
		
		params.put("id", model.getUuid());
		
		JSONObject res=new JSONObject();
		
		try {
			News news=service.findByHql(hql, params);
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "查询成功");
			
			JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
			  jsonConfig.setIgnoreDefaultExcludes(false);  //设置默认忽略
			  jsonConfig.setExcludes(new String[]{"img"});
			  
			res.put("data", JSONObject.fromObject(news, jsonConfig));
		} catch (SQLException e) {
			e.printStackTrace();
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "查询失败");
		}
		CommonUtils.writeJson(res);
	}
	public void del() throws SQLException{
		boolean flag=service.deleteIds(ids);
		JSONObject res=new JSONObject();
		if(flag){
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "删除数据成功");
		}else{
			res.put(RES_CODE, -100); 
			res.put(RES_MSG, "删除数据失败");
		}
		try {
			operationLogService.save(getOperationLogBean("delete news",new String[]{"content","sumary","subtitle"}));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		CommonUtils.writeJson(res);
	}
	
	private String imgid;
	public String getImgid() {
		return imgid;
	}
	public void setImgid(String imgid) {
		this.imgid = imgid;
	}

}
